package frontOffice;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;

import org.apache.catalina.Session;
import org.apache.catalina.connector.Request;

import rai.RaiClientListeRepertoires;
import rai.UtilisateurInconnueException;
import rai.service.rep.ListeRepertoires;

public class Login extends HttpServlet {
	HttpSession laSession;
	private int portServerAnnuaire;
	private String hostServerAnnuaire;

	public void init(ServletConfig config) {
		hostServerAnnuaire = (String) config.getInitParameter("host");
		String nomPortServerAnnuaire = (String) config.getInitParameter("port");
		portServerAnnuaire = Integer.parseInt(nomPortServerAnnuaire);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		laSession = request.getSession(true);
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		laSession.setAttribute("leLogin", login);
		laSession.setAttribute("leHost", hostServerAnnuaire);
		laSession.setAttribute("lePort", portServerAnnuaire);
		ListeRepertoires laConnexion = null;
		PrintWriter out;
		try {
			laConnexion = connectAnnuaire(login, password);
			laSession.setAttribute("laConnexion", laConnexion);
			String nonMachineServer = request.getServerName();
			int portServer = request.getServerPort();
			String[] laListeDesAnnuaires = laConnexion.listerRepertoires();
			out = response.getWriter();

			try {
				response.setContentType("text/html");
				out
						.println("<HTML>\n<HEAD>  <link href=\"style/style.css\" rel=\"stylesheet\" type=\"text / css\" /></HEAD>");
				out.println("<BODY>\n" + "<H1>" + "CAR-WebAnnuaire"
						+ " </H1><br><br>" + login
						+ " qui est connecté sur la machine "
						+ nonMachineServer + ":" + portServer
						+ " est maintenant connecté au serveur annuaire "
						+ this.hostServerAnnuaire + ":"
						+ this.portServerAnnuaire
						+ "<br><br><H2>Listes des repertoires</H2>\n"
						+ "Il y a " + laListeDesAnnuaires.length
						+ " repertoire(s)<br>");
				for (String repertoire : laListeDesAnnuaires) {
					out.println("<LI><a href=\"ManageRepertoire?nomRep="
							+ repertoire + "\">" + repertoire + "</a>");
				}
				out.println("<br><a></BODY></HTML>");
				out
						.println("</div>"
								+ "<FORM method=get action=\"ManageRepertoire\">"
								+ "<div id=\"boutonsRepertoire\"> <p>"
								+ "<input type=\"submit\" name=\"actionRep\" value=\"ajouter\">"
								+ "<div id=\"tableau\"><TABLE border=\"1\">"
								+ "<TR>"
								+ "<TD>nom :</TD><TD><INPUT type=text name=\"nomRep\"/></TD>"
								+ "<TR></TABLE></FORM></div>");
			} finally {
				out.close();
			}
		} catch (UtilisateurInconnueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			erreur(request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void erreur(HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			response.setContentType("text/html");
			out
					.println("<HTML>\n<HEAD>  <link href=\"style/style.css\" rel=\"stylesheet\" type=\"text / css\" /></HEAD>");
			out
					.println("<BODY onload=\"alert('Utilisateur inconnu');window.location.replace('index')\">");
			out.println("<br></BODY></HTML>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.close();
		}
	}

	private void pageError(String printStackTrace) {
		// TODO Auto-generated method stub

	}

	private ListeRepertoires connectAnnuaire(String login, String password)
			throws IOException, UtilisateurInconnueException {
		ListeRepertoires lesAnuaires = new RaiClientListeRepertoires(
				hostServerAnnuaire, portServerAnnuaire);
		lesAnuaires.authentification(login, password);
		return lesAnuaires;

	}
}
